<html>

<head>
<title>Ankiety</title>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">

</head>

<body>
	<?php 
	$link = mysql_connect('localhost', 'root', '', 'ankiety')
    	or die('Could not connect: ' . mysql_error());
	echo "<br>";
	mysql_select_db('ANKIETY') or die('Could not select database');

	session_start();
	echo "Odpowiedzi: <br>";
	if(isset($_GET['dodaj_odpowiedz']))
	{
		$_SESSION['odpowiedzi']=$_SESSION['odpowiedzi']+1;
		echo "ustawienie: <br>";
	}
	echo "po ustawieniu: <br>";
	if(isset($_POST['dodaj']) || isset($_GET['dodaj_odpowiedz']))
	{
		echo "<h2>Dodajesz nową ankietę:</u></h2>";
		echo "Pytanie: ";
		echo '<form name="dodaj_ankiete" method="get" action="admin2.php">';
		echo "<p><input name='pytanie' type='text' value='' size='100' /> </p>";
		echo "</form>";
		
		echo "Odpowiedzi: ";
		echo '<form name="odpowiedz" method="get" action="admin2.php">';
		for ($i=1; $i <= $_SESSION['odpowiedzi']; $i++) 
				{ 
					echo "<p> ".$i.": <input name=".$i." type='text' value='' size='40' /> </p>";
				}
		echo "</form>";
		echo '<form name="dodaj_odpowiedz" method="get" action="admin2.php">';
		echo "<p><input name='dodaj_odpowiedz' type='submit' value='Dodaj odpowiedź' /> </p>";
		echo "</form>";
		
		
		$query="select * from ".$tabela."";
		$result=mysql_query($query);
		$numfields = mysql_num_fields($result);

		echo "<table>\n<tr>";

		for ($i=0; $i < $numfields; $i++) 
		{ echo '<th>'.mysql_field_name($result, $i).'</th>'; }

		echo "</tr>\n";

		while ($row = mysql_fetch_row($result)) 
		{ echo '<tr><td>'.implode($row,'</td><td>')."</td></tr>\n"; }

		echo "</table>\n";
		
		$form_add = "lista7_admin.php?tab=".$tabela;
		
		if(!isset($_POST['znak']) && !isset($_POST['col0']))
		{
			?>
			<form name="dzialanie" method="post" action="<?php $form_add ?>" >
			<?php echo "<h2>Co chcesz zrobic?</h2>"; ?>
			<input type="radio" name="znak" value="+" /> Dodac dane do tabeli <br>
			<input type="radio" name="znak" value="-" /> Usunac dane z tabeli <br>
			<p><input name="modyfikacja" type="submit" value="Wybierz" /> </p>
			</form>
			<?php
		}
		if(isset($_POST['znak']) && !isset($_POST['col0']))
		{
			if($_POST['znak']=="+")
			{
				echo "<h2>Dodawanie danych</h2>";
				echo "<table>\n<tr>";
				for ($i=0; $i < $numfields; $i++) 
				{ 
					echo '<th>'.mysql_field_name($result, $i).'</th>';
				}
				echo "</tr>\n";
				echo "<tr>\n";
				
				echo "<form action='".$form_add."' method='post' name='insert_form'>";
				$arr1 = array();
				for ($i=0; $i < $numfields; $i++) 
				{ 
					$form_name = "arr1[".$i."]";
					echo "<td><input maxlength='500' name='".$form_name."' size='40' type='text' /></td>"; 
				}
				
				echo "</tr>\n";
				echo "</table>\n";
				echo "<p><input name='insert' type='submit' value='Dodaj' /> </p>";
				
			}
			if($_POST['znak']=="-")
			{
				$query="select * from ".$tabela."";
				$result=mysql_query($query);
				echo "<h2>Usuwanie danych</h2>";
				echo "<p>Wybierz wartosc ".mysql_field_name($result, 0)." dla wiersza, ktory chcesz usunac</p>";
				echo "<form name='usuwanie' method='post' action='".$form_add."'>";
				echo "<select name='usun'>";
				while ($row = mysql_fetch_array($result))
				{
					echo "<option value=$row[0]>$row[0]</option>";
				}
				echo "</select>";
				echo "<p><input name='delete' type='submit' value='Usun' /> </p>";
				echo "</form>";
			}
		}
		

		if(isset($_POST['arr1'][0]))
		{
			$arr2 = array();
			for ($i=0; $i < $numfields; $i++) 
			{ 
				$arr2[$i]=mysql_field_name($result, $i);
			}
			$arr3 = array();
			for ($i=0; $i < $numfields; $i++) 
			{ 
				$arr3[$i]=$_POST['arr1'][$i];
			}
			if ($numfields==3) 
				$sql="INSERT INTO ".$tabela." (".$arr2[0].", ".$arr2[1].", ".$arr2[2].")
				VALUES ('".$arr3[0]."', '".$arr3[1]."', '".$arr3[2]."')";
			if ($numfields==4) 
				$sql="INSERT INTO ".$tabela." (".$arr2[0].", ".$arr2[1].", ".$arr2[2].", ".$arr2[3].")
				VALUES ('".$arr3[0]."', '".$arr3[1]."', '".$arr3[2]."', '".$arr3[3]."')";
			if ($numfields==5) 
				$sql="INSERT INTO ".$tabela." (".$arr2[0].", ".$arr2[1].", ".$arr2[2].", ".$arr2[3].", ".$arr2[4].")
				VALUES ('".$arr3[0]."', '".$arr3[1]."', '".$arr3[2]."', '".$arr3[3]."', '".$arr3[4]."')";
			$con = mysqli_connect('hosting.we.po.opole.pl', 's81402', 'keram1991', 's81402');
			if (!mysqli_query($con,$sql))
			{
			  die('Error: ' . mysqli_error($con));
			}
			echo "<h2>Dane zostaly dodane</h2>";
		}
		if(isset($_POST['usun']))
		{
			$sql="DELETE FROM ".$tabela." WHERE ".mysql_field_name($result, 0)."=".$_POST['usun'];
			$con = mysqli_connect('hosting.we.po.opole.pl', 's81402', 'keram1991', 's81402');
			echo "<br>";
			if (!mysqli_query($con,$sql))
			{
			  die('Error: ' . mysqli_error($con));
			}
			echo "<h2>Dane zostaly usuniete</h2>";
		}
	}
	?>
</body>

</html>
